package com.jereh.topnews.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.jereh.topnews.R;
import com.jereh.topnews.db.ChannelDao;
import com.jereh.topnews.entity.Channel;
import com.jereh.topnews.fragment.ConnactListFragment;
import com.jereh.topnews.fragment.NewsListFragment;
import com.jereh.topnews.fragment.UserCenterFragment;
import com.jereh.topnews.fragment.VideoListFragment;

import java.util.ArrayList;

public class NewsMainActivity extends AppCompatActivity {
    private FragmentTabHost fragmentTabHost;
    private String[] titles={"首页","视频","关注","未登录"};
    private int imageButton[] = { R.drawable.home_news_tabbar_select,
            R.drawable.home_video_tabbar_select,
            R.drawable.home_care_tabbar_select,
            R.drawable.home_user_tabbar_select };
    private Class fragmentArray[] = {NewsListFragment.class,VideoListFragment.class,
            ConnactListFragment.class,UserCenterFragment.class};
    public ArrayList<Channel> list=new ArrayList<>();
    private ChannelDao channelDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        channelDao=new ChannelDao(this);
        initFragmentTabHost();
        getChannelList();
    }
    private void initFragmentTabHost(){
// 实例化tabhost
        fragmentTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(),R.id.fragcontent);
        for (int i = 0; i < titles.length; i++) {
            TabHost.TabSpec spec=fragmentTabHost.newTabSpec(titles[i]).setIndicator(getView(i));
            fragmentTabHost.addTab(spec, fragmentArray[i], null);
        }
        ((TextView)fragmentTabHost.getTabWidget().getChildAt(0).findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.topRed));
//        //在tabhost中获取fragment实例
//        NewsListFragment fragment=(NewsListFragment)getSupportFragmentManager().findFragmentByTag(titles[0]);
//        //把数据封装到bundle对象中
//        Bundle b=new Bundle();
//        b.putSerializable("channel",list);
//        //向fragment中传递数据
//        fragment.setArguments(b);
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                fragmentTabHost.setCurrentTabByTag(tabId);
                for (int i = 0; i < fragmentTabHost.getTabWidget().getChildCount(); i++) {
                    View view=fragmentTabHost.getTabWidget().getChildAt(i);
                    if (fragmentTabHost.getCurrentTab() == i) {//选中
                        ((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.topRed));
                    } else {
                        ((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.bottomTextGray));
                    }
                }

            }
        });
    }

    /**
     * 获取从数据库中查询出的频道信息
     */
    private void getChannelList() {
        list.addAll(channelDao.findAll());
    }

    /**
     * tab的样式
     * @param i
     * @return
     */
    private View getView(int i) {
        //取得布局实例
        View view=View.inflate(NewsMainActivity.this, R.layout.tabhost_content, null);

        //取得布局对象
        ImageView imageView=(ImageView) view.findViewById(R.id.logo);
        TextView textView=(TextView) view.findViewById(R.id.title);

        //设置图标
        imageView.setImageResource(imageButton[i]);
        //设置标题
        textView.setText(titles[i]);
//        if (fragmentTabHost.getCurrentTab()==i) {
//            textView.setTextColor(getResources().getColor(R.color.topRed));
//        } else {
//            textView.setTextColor(getResources().getColor(R.color.bottomTextGray));
//        }
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        channelDao.closeDB();
    }
}
